package com.yy.job.core.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.yy.job.common.dao.po.YYJobDO;
import com.yy.job.common.enums.JobExecuteTypeEnum;
import com.yy.job.common.enums.JobStatusEnum;

/**
 * 任务建模域
 * 
 * @author eleven.li
 *
 */
public class JobDomain extends BaseDomain {

	private static final long serialVersionUID = 7044027972573316301L;
	
	/**
	 * 项目code
	 */
	private String projectCode;
	
	/**
	 * 模块组
	 */
	private String moduleCode;

	private String moduleName;
	
	/**
	 * 任务code
	 */
	private String jobCode;

	private String jobName;
	
	/**
	 * 任务状态
	 */
	private JobStatusEnum jobStatus;
	
	/**
	 * 业务号
	 */
	private String businessNo;
	
	/**
	 * 期望开始执行时间
	 */
	private Date expectedStartTime;
	
	/**
	 * 执行时间
	 */
	private Date execTime;
	
	/**
	 * 总共执行次数
	 */
	private Integer execCount;
	
	/**
	 * 版本号，乐观锁
	 */
	private Integer version;

//	private String creator;
	/**
	 * 创建时间
	 */
	private Date createdTime;
	
	/**
	 * 修改时间
	 */
	private Date modifiedTime;
	
	/**
	 * 步骤列表
	 */
	private List<JobStepDomain> jobStepDomains;
	
	/**
	 * 任务执行类型
	 */
	private JobExecuteTypeEnum jobExecuteType = JobExecuteTypeEnum.STANDARD;
	
	/**
	 * yy-job的自启动处理次数
	 */
	private int runCount;

	public JobDomain() {

	}

	public JobDomain(YYJobDO jobDO) {
		this.setId(jobDO.getId());
		this.setProjectCode(jobDO.getProjectCode());
		this.setModuleCode(jobDO.getModuleCode());
		this.setModuleName(jobDO.getModuleName());
		this.setJobCode(jobDO.getJobCode());
		this.setJobName(jobDO.getJobName());
		if (StringUtils.isNotBlank(jobDO.getJobStatus())) {
			this.setJobStatus(JobStatusEnum.findByCode(jobDO.getJobStatus()));
		}
		this.setJobExecuteType(JobExecuteTypeEnum.findByCode(jobDO.getExecuteType()));
		this.setBusinessNo(jobDO.getBusinessNo());
		this.setExpectedStartTime(jobDO.getExpectedStartTime());
		this.setExecTime(jobDO.getExecTime());
		this.setExecCount(jobDO.getExecCount());
		this.setVersion(jobDO.getVersion());
		//setCreator(jobDO.getCreator());
		setCreatedTime(jobDO.getGmtCreated());
		this.setModifiedTime(jobDO.getGmtModified());
		this.setOperator(jobDO.getModifier());
	}
	
	public String getProjectCode() {
		return projectCode;
	}

	public void setProjectCode(String projectCode) {
		this.projectCode = projectCode;
	}

	public String getModuleCode() {
		return this.moduleCode;
	}

	public void setModuleCode(String moduleCode) {
		this.moduleCode = moduleCode;
	}

	public String getModuleName() {
		return moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public String getJobCode() {
		return this.jobCode;
	}

	public void setJobCode(String jobCode) {
		this.jobCode = jobCode;
	}

	public String getJobName() {
		return this.jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public JobStatusEnum getJobStatus() {
		return this.jobStatus;
	}

	public void setJobStatus(JobStatusEnum jobStatus) {
		this.jobStatus = jobStatus;
	}

	public Date getExecTime() {
		return this.execTime;
	}

	public void setExecTime(Date execTime) {
		this.execTime = execTime;
	}

	public Integer getExecCount() {
		return this.execCount;
	}

	public void setExecCount(Integer execCount) {
		this.execCount = execCount;
	}

	public String getBusinessNo() {
		return this.businessNo;
	}

	public void setBusinessNo(String businessNo) {
		this.businessNo = businessNo;
	}

	public List<JobStepDomain> getJobStepDomains() {
		return this.jobStepDomains;
	}

	public Date getExpectedStartTime() {
		return expectedStartTime;
	}

	public void setExpectedStartTime(Date expectedStartTime) {
		this.expectedStartTime = expectedStartTime;
	}

	public Date getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Date getModifiedTime() {
		return modifiedTime;
	}

	public void setModifiedTime(Date modifiedTime) {
		this.modifiedTime = modifiedTime;
	}

	public JobExecuteTypeEnum getJobExecuteType() {
		return jobExecuteType;
	}

	public void setJobExecuteType(JobExecuteTypeEnum jobExecuteType) {
		this.jobExecuteType = jobExecuteType;
	}

	public int getRunCount() {
		return runCount;
	}

	public void setRunCount(int runCount) {
		this.runCount = runCount;
	}

	public void addJobStepDomain(JobStepDomain jobStepDomain) {
		if (jobStepDomain == null) {
			return;
		}
		if (this.jobStepDomains == null) {
			this.jobStepDomains = new ArrayList<>();
		}
		this.jobStepDomains.add(jobStepDomain);
		jobStepDomain.setJobId(getId());
		jobStepDomain.setJobDomain(this);
	}

	public void setJobStepDomains(List<JobStepDomain> stepDomains) {
		if (stepDomains == null) {
			return;
		}
		this.jobStepDomains = stepDomains;
		for (JobStepDomain stepDomain : stepDomains) {
			if (stepDomain != null && stepDomain.getJobDomain() == null) {
				stepDomain.setJobDomain(this);
				stepDomain.setJobId(getId());
			}
		}
	}

	@Override
	public String toString() {
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("id", this.getId());
		jsonObj.put("projectCode", this.projectCode);
		jsonObj.put("moduleCode", this.moduleCode);
		jsonObj.put("jobCode", this.jobCode);
		jsonObj.put("businessNo", this.businessNo);
		jsonObj.put("jobStatus", this.jobStatus);
		return jsonObj.toJSONString();
	}

}
